<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>PHP deserializer</title>
  <style>
  * {
    box-sizing: border-box;
  }

  body {
    font-family: Helvetica, Arial, sans-serif;
    line-height: 1.5;
    max-width: 800px;
    margin: 0 auto;
    padding: 1rem;
  }

  h1 {
    margin-top: 0;
  }

  textarea {
    width: 100%;
    min-height: 200px;
    margin-bottom: 1rem;
    padding: 0.5rem;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 16px;
    font-family: monospace;
  }

  button {
    background: #0066cc;
    color: white;
    border: none;
    padding: 0.5rem 1rem;
    border-radius: 4px;
    font-size: 16px;
    cursor: pointer;
  }

  button:disabled {
    background: #ccc;
    cursor: not-allowed;
  }

  .error {
    color: #cc0000;
    background: #ffebeb;
    padding: 1rem;
    border-radius: 4px;
    margin-bottom: 1rem;
    display: none;
  }

  .output-container {
    display: none;
    margin-bottom: 1rem;
  }

  .copy-button {
    margin-bottom: 1rem;
  }
  </style>
</head>
<body>
  <h1>PHP deserializer</h1>
  <p>Paste serialized PHP data below to convert it to JSON</p>
  
  <textarea id="input" placeholder="Paste serialized PHP data here"></textarea>
  
  <div id="error" class="error"></div>
  
  <div id="output-container" class="output-container">
    <button id="copy" class="copy-button">Copy JSON to clipboard</button>
    <textarea id="output" readonly></textarea>
  </div>

<script type="module">
import * as phpSerialize from 'https://cdn.jsdelivr.net/npm/php-serialize@5.0.1/+esm'

const input = document.getElementById('input')
const output = document.getElementById('output')
const error = document.getElementById('error')
const copyButton = document.getElementById('copy')
const outputContainer = document.getElementById('output-container')

function updateOutput() {
  const serialized = input.value.trim()
  error.style.display = 'none'
  outputContainer.style.display = 'none'
  
  if (!serialized) {
    return
  }

  try {
    const parsed = phpSerialize.unserialize(serialized)
    const json = JSON.stringify(parsed, null, 2)
    output.value = json
    outputContainer.style.display = 'block'
  } catch (err) {
    error.textContent = `Error: ${err.message}`
    error.style.display = 'block'
  }
}

input.addEventListener('input', updateOutput)

copyButton.addEventListener('click', async () => {
  try {
    await navigator.clipboard.writeText(output.value)
    const originalText = copyButton.textContent
    copyButton.textContent = 'Copied!'
    copyButton.disabled = true
    setTimeout(() => {
      copyButton.textContent = originalText
      copyButton.disabled = false
    }, 2000)
  } catch (err) {
    error.textContent = `Error copying to clipboard: ${err.message}`
    error.style.display = 'block'
  }
})
</script>
</body>
</html>
